/**
 * 
 */
package kr.co.insoft.auth.mapper.security;

import java.util.List;

import kr.co.insoft.auth.mapper.initialize.InitializeTableMapper;
import kr.co.insoft.auth.model.initialize.InitializeTableModel;
import kr.co.insoft.auth.model.security.Role;
import kr.co.insoft.auth.model.security.User;
import kr.co.insoft.auth.service.initalize.InitializeTableService;
import kr.co.insoft.framework.mybatis.Mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

/**
 * @author IN-SOFT Inc. GoodwillDD(sylee@in-soft.co.kr)
 * 
 */
@Mapper
public interface UserDetailsMapper extends InitializeTableMapper {

	@Select("SELECT `id`, `username`, `nickname`, `password`, `accountNonExpired`, `accountNonLocked`, `credentialsNonExpired`, `enabled`  FROM `"
			+ InitializeTableService.USERS_TABLE
			+ "` WHERE username = #{userName}")
	public User findByUsername(String username) throws Exception;

	@Override
	@Select("SELECT count(*) as `count` FROM information_schema.tables WHERE `TABLE_SCHEMA` = #{datasource} AND `TABLE_NAME` = #{tablename}")
	public Integer isExistTable(InitializeTableModel initializeTableModel)
			throws Exception;

	@Override
	@Insert("CREATE TABLE `"+InitializeTableService.USERS_TABLE+"` (`id` BIGINT(20) NOT NULL AUTO_INCREMENT,`username` VARCHAR(255) NOT NULL,`nickname` VARCHAR(255) NOT NULL,`password` VARCHAR(255) NOT NULL,`createdTime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,`accountNonExpired` BIT(1) NOT NULL DEFAULT b'0',`accountNonLocked` BIT(1) NOT NULL DEFAULT b'0',`credentialsNonExpired` BIT(1) NOT NULL DEFAULT b'0',`enabled` BIT(1) NOT NULL DEFAULT b'0',PRIMARY KEY (`id`),INDEX `username` (`username`))")
	public void createTable() throws Exception;

	@Select("SELECT `roleName` as 'role' FROM `"+InitializeTableService.USER_ROLES+"` WHERE `userid`=#{id}")
	public List<Role> findRoleByUserid(Long id) throws Exception;
}
